package com.fh.shop.api.pay.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.config.AlipayConfig;
import com.fh.shop.api.pay.biz.IPayService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@RestController
@RequestMapping("/api/pay")
public class PayController {

    @Resource(name = "payService")
    private IPayService payService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private HttpServletResponse response;

    @Value("${pay.success}")
    private String paySuccess;
    @Value("${pay.fail}")
    private String payFail;


    @GetMapping
    public String alipay(String orderId){
        return payService.alipay(orderId);
    }


    @PostMapping("/aliNotify")
    public String aliNotify(){
        //获取所有的参数信息
        Map<String,String[]> requestParams = request.getParameterMap();
        return payService.aliNotify(requestParams);
    }


    @GetMapping("/aliReturn")
    //支付成功的时候，支付宝平台才会调用 回调项目中的 /aliReturn 接口
    public void returnUrl(){
        try {
            //获取支付宝GET过来反馈信息
            Map<String,String> params = new HashMap<String,String>();
            //支付宝平台调用你的 returnUrl，所有获取支付宝平台传递过来的参数
            Map<String,String[]> requestParams = request.getParameterMap();
            for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
                String name = (String) iter.next();
                String[] values = (String[]) requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i]
                            : valueStr + values[i] + ",";
                }
                //乱码解决，这段代码在出现乱码时使用
                valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
                params.put(name, valueStr);
            }
            //验签
            boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名

            if(signVerified) {
                // 验签成功，数据没有被篡改，支付
                response.sendRedirect(paySuccess);
            } else {
                //验签失败
                response.sendRedirect(payFail);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
